﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Doo_JetDAL;
using DoodJetView.Core.Client;
using DoodJetView.Core;

namespace DoodJetView.VuesPartielles
{
    /// <summary>
    /// Logique d'interaction pour EmployerForm.xaml
    /// </summary>
    public partial class ClientForm : UserControl
    {

        public ClientForm()
        {
            InitializeComponent();
            LoadDataForForm();
        }
        public ClientForm(string nom, string prenom, string adresse, string adresse2, string ville, string tel, string dateNaiss, string permisCotier)
        {
            InitializeComponent();
            txt_nom.Text = nom;
            txt_prenom.Text = prenom;
            txt_adresse1.Text = adresse;
            txt_adresse2.Text = adresse2;
            //txt_ville.Text = cli.ville;
            //txtb_telephone.Text = cli.telClient;
            //txt_Dat_Naiss.Text = cli.dateNaissance;
            //txt_permis_bateau.Text = permisCotier;
        }
        public ClientForm(client cli, string titre, string mode)
        {
            InitializeComponent();
            this.LoadDataForForm();
            this.ClientElement = cli;
            txt_nom.Text = cli.nomClient;
            txt_prenom.Text = cli.prenomClient;
            txt_adresse1.Text = cli.adresseClient;
            txt_adresse2.Text = cli.adresse2Client;
            listBoxVille.SelectedIndex = (int)cli.id_ville;
            //txt_ville.Text = cli.id_client;
            txt_telephone.Text = cli.telClient.ToString();
            Picker_Dat_Naiss.SelectedDate = DateTimeUnixTools.ConvertFromUnixTimestamp(cli.dateNaissance);
            //txt_permis_bateau.Text = cli.permisCotier.ToString();
            lbl_titre.Content = titre;
            if (mode == "Show")
            {
                btn_Submit.Visibility = Visibility.Hidden;
            }
            if (cli.permis != null)
            {
                this.formPermis = new Permis_Form(false,cli.permis);
            }
        }
        public void ClearData() {
            txt_nom.Text = String.Empty;
            txt_prenom.Text = String.Empty;
            txt_adresse1.Text = String.Empty;
            txt_adresse2.Text = String.Empty;
            listBoxVille.Name = String.Empty;
            //txtb_telephone.Text = cli.telClient;
            //txt_Dat_Naiss.Text = cli.dateNaissance;
            //txt_permis_bateau.Text = String.Empty;
        }
        private void LoadDataForForm()
        {
            //Chargement des Activités 
            using (DataEntities db = new DataEntities())
            {
                try
                {
                    List<maps_ville> ville = db.maps_ville.ToList();
                    foreach (maps_ville v in ville)
                    {
                        var Paco = new maps_ville_poco { ID = (int)v.id_ville, Nom = v.nom };
                        this.listBoxVille.Items.Add(Paco);
                    }
                }
                catch (Exception e)
                {
                    throw(e);
                }
                

            }
        }
        private void btn_Submit_Click(object sender, RoutedEventArgs e)
        {
            //Si l'élément de données est null
            if (this.ClientElement == null)
            {
                using (DataEntities cont = new DataEntities())
                {
                    var idTypeVille = listBoxVille.SelectedItem as maps_ville_poco;
                    maps_ville currenttypeVille = cont.maps_ville.Single(o => o.id_ville == idTypeVille.ID);
                    //Supêr Nico :)
                    var clie = new client
                    {
                        nomClient = txt_nom.Text,
                        prenomClient = txt_prenom.Text,
                        adresseClient = txt_adresse1.Text,
                        adresse2Client = txt_adresse2.Text,
                        dateNaissance = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)Picker_Dat_Naiss.SelectedDate),
                        telClient = int.Parse(txt_telephone.Text),
                        maps_ville = currenttypeVille
                    };
                    if ((bool)hasPermis.IsChecked == true)
                    {
                        clie.permis = this.formPermis.FromView();
                    }
                    try { cont.client.DeleteObject(clie); }
                    catch (Exception ex) { }
                    cont.client.AddObject(clie);

                    cont.SaveChanges();
                    int lastpermis = (int)cont.permis.OrderBy(o => o.id_permis).ToList().Last().id_permis;

                }
                MessageBox.Show("Enregistrement :)", "ToDO", MessageBoxButton.OK);
            }
            else
            { 
                   using (DataEntities cont = new DataEntities())
                {

                    client c = cont.client.Where(o => o.id_client == this.ClientElement.id_client).First();
                       c.nomClient = txt_nom.Text;
                        c.prenomClient = txt_prenom.Text;
                        c.adresseClient = txt_adresse1.Text;
                        c.adresse2Client = txt_adresse2.Text;
                        c.dateNaissance = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)Picker_Dat_Naiss.SelectedDate);
                        c.telClient = int.Parse(txt_telephone.Text);
                       var idTypeVille = listBoxVille.SelectedItem as maps_ville_poco;
                       maps_ville currenttypeVille = cont.maps_ville.Single(o => o.id_ville == idTypeVille.ID);
                        c.maps_ville = currenttypeVille;
                       cont.SaveChanges();
                   }
                
            }

        }
        private Permis_Form formPermis;
        private void hasPermis_Checked(object sender, RoutedEventArgs e)
        {
            if ((bool)hasPermis.IsChecked)
            {
                if (this.formPermis == null)
                    this.formPermis = new Permis_Form(false);
                gridpermis.Children.Clear();
                gridpermis.Children.Add(this.formPermis);

                gridpermis.Visibility = System.Windows.Visibility.Visible;
            }
            else {
                gridpermis.Visibility = System.Windows.Visibility.Hidden;
            }

        }

        public client ClientElement { get; set; }
    }
}
